package com.snapp.web.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.jdom.Element;

import com.snapp.web.SNAppAction;
import com.snapp.web.agents.SNAppAgent;

public class Count extends SNAppAction {
	private int count = 0;
	
	public String execute(HttpServletRequest req) {
		setSNAppGlobalId(req.getParameter("SNAppGlobalId"));
		if (getSNAppGlobalId() == null) {
			setResultCode(-1);
			setResultMessage("Missing \"SNAppGlobalId\" parameter.  Not sure which object to load.");
			return super.execute(req);
		}
		try {
			SNAppAgent agent = new SNAppAgent();
			count = agent.count(getSNAppGlobalId(), consolidateReadMap(req));
			
			setResultCode(1);
			setResultMessage("Object list loaded successfully.");

			Element count_xml = new Element("resp").setAttribute("count",Integer.toString(count)); 
			setBodyElement(count_xml);
		
		} catch(Exception e) {
			log("Error parsing dbo bean to xml.", e);
		}
		
		return super.execute(req);
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

}
